<template>
	<view class="category_tab">
		<view class="category_tab_title">
			<view class="title_inner">
				<uni-segmented-control
				:current="current" 
				:values="items.map(v => v.title)" @clickItem="onClickItem"
				styleType="text" activeColor="#d3237b"
				></uni-segmented-control>
			</view>
			<view class="iconfont icon-search1">	
			</view> 
		</view>
		 <scroll-view enable-flex scroll-y 
				@scrolltolower='handleScrollToLower'
				class="category_tab_content">
				<view class="cate_item"
				v-for='item in vertical'
				:key='item.id'
				>
				<image :src="item.thumb" mode="widthFix"></image>
				</view>
		</scroll-view>
	</view>
</template>

<script>
	export default {
	data() {
		return { 
			items: [{title: '最新', order: 'new'},{title: '热门', order: 'hot'	}],
			current:0,
			params: {
				limit: 30,
				skip: 0,
				order: 'new'
			},
			id: 0,
			//页面显示数据
			vertical: [],
			hasMore: true
		} 
	}, 
	onLoad(options) {
		this.id = options.id
		this.getList()
	},
		methods: {
		onClickItem(index) {
				if (this.current !== index.currentIndex) { 
					this.current = index.currentIndex;
				} else {
					//重复点击了相同标题
					return;
				}
				this.params.order = this.items[index.currentIndex].order
				this.params.skip = 0;
				this.vertical = []
				this.getList()
			},
		async	getList() {
		const result = await this.request({
			url: `http://157.122.54.189:9088/image/v1/vertical/category/${this.id}/vertical`,
			data: this.params
		})
			if(result.res.vertical.length === 0) {
				this.hasMore = false
				uni.showToast({
					title: '没有更多数据',
					icon: 'none'
				})
				return;
			}
			this.vertical = [...this.vertical, ...result.res.vertical]
		},
		// 加载下一页
		handleScrollToLower() {
			if(this.hasMore) {
					this.params.skip += this.params.limit
					this.getList()
			}else {
				uni.showToast({
					title: '没有更多数据',
					icon: 'none'
				})
			}
		},
		}
	}
</script>

<style lang="less">
.category_tab {
		.category_tab_title {
			position: relative;
			.title_inner {
				width: 60%;
				margin: 0 auto;
			}
			.icon-search1{
				position: absolute;
				top: 50%;
				transform: translateY(-50%);
				right: 5%;
			}
		}
		.category_tab_content {
			display: flex;
			flex-wrap: wrap;
			height: calc(100vh - 72rpx);
			.cate_item {
				width: 33.33%;
				border: 5rpx solid #fff;
				image {
				
				}
			}
		}
	}
</style>
